﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class MyCreditsPerVendor : AuthenticationRequiredCustomerBaseUserControl
	{
		private uint vendorId;
		VendorInfo vendorInfo = null;

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected override void PreLoadHandler(object sender, EventArgs e)
		{
			try
			{
				vendorId = Convert.ToUInt32(this.Request["v"]);

				vendorInfo = VendorInfo.GetVendorInfo(vendorId);

				this.lblVendorName.Text = vendorInfo.BedrijfsNaam;

				this.VendorPresenterFull.VendorId = vendorId;
			}
			catch
			{
				Response.End();
			}

		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			//decimal totSaldo;
			//decimal totStartSaldo;
			//decimal totUitgaven;

			//this.CreditPerVendor.Sum(out totStartSaldo, out totSaldo, out totUitgaven);

			//tbTotSaldo.Value = ObjectFormatter.GetCurrencyString(totSaldo);
			//tbTotStartSaldo.Value = ObjectFormatter.GetCurrencyString(totStartSaldo);
			//tbTotUitgaven.Value = ObjectFormatter.GetCurrencyString(totUitgaven);

			this.GridView1.DataSource = this.CreditPerVendor;

			this.GridView1.DataBind();

			this.VendorPresenterFull.VendorId = vendorId;

			this.hplOrderMore.NavigateUrl = "/OffersPerVendor.aspx?v=" + vendorId.ToString();
			this.hplOrderMore.Text = "Bestel een nieuwe deal bij " + vendorInfo.BedrijfsNaam;

			this.CustomizeView();
		}

		private void CustomizeView()
		{
			foreach (GridViewRow row in this.GridView1.Rows)
			{
				HiddenField d = row.Cells[0].FindControl("fldDescription") as HiddenField;
				if (d.Value.Length > 0)
				{
					Image img = new Image();
					img.ImageUrl = "/Images/Warning32x32.png";
					img.ToolTip = d.Value;
					row.Cells[0].Controls.Add(img);
				}
			}
		}

		private UserActiveOfferdetailsPerVendor CreditPerVendor
		{
			get
			{
				UserActiveOfferdetailsPerVendor details = UserActiveOfferdetailsPerVendor.Instance(this.UserInfo.Id, this.vendorId);

				return details;
			}
		}


		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				string offerId = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

				TransactionHistory t = this.CreditPerVendor.Transactions[Convert.ToUInt32(offerId)];

				if (t.Total == 0)
				{
					Control expandCollapseImage = e.Row.FindControl("pnlExpandCollapseImage");
					if (expandCollapseImage != null)
						expandCollapseImage.Visible = false;
				}
				else
				{
					GridView grdTransactions = (GridView)e.Row.FindControl("grdViewTransactions");
					grdTransactions.DataSource = t;
					grdTransactions.DataBind();
				}
			}
		}

		protected void GridView1_DataBinding(object sender, EventArgs e)
		{
			decimal totSaldo;
			decimal totStartSaldo;
			decimal totUitgaven;

			this.CreditPerVendor.Sum(out totStartSaldo, out totSaldo, out totUitgaven);

			this.GridView1.Columns[3].FooterText = ObjectFormatter.GetCurrencyString(totStartSaldo);
			this.GridView1.Columns[4].FooterText = ObjectFormatter.GetCurrencyString(totSaldo);
			this.GridView1.Columns[5].FooterText = ObjectFormatter.GetCurrencyString(totUitgaven);
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			this.GridView1.DataSource = this.CreditPerVendor.Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataBind();

			this.VendorPresenterFull.VendorId = vendorId;
		}
	}
}